package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckgroupMapper;
import com.itheima.health.dao.SetmealCheckgroupMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCheckgroup;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import sun.rmi.runtime.Log;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    /**
     * @Author: 15041
     * @Date: 10:29 2021/1/11
     * @Parms: [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     *Description: 分页查询套餐
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("套餐-套餐分页查询service层传参：{}",queryPageBean);
        Page<Object> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //创建Example对象，传入要查询的表的实体类
        Example example = new Example(Setmeal.class);
        //调用createCriteria方法
        Example.Criteria criteria = example.createCriteria();
        String queryString = "%"+queryPageBean.getQueryString()+"%";
        //判断条件是否为空
        if (StringUtils.isNotEmpty(queryPageBean.getQueryString())){
            criteria.orLike("name",queryString);
            criteria.orLike("code",queryString);
            criteria.orLike("helpCode",queryString);
        }

        Page<Setmeal> list = (Page<Setmeal>) setmealMapper.selectByExample(example);

        long total = page.getTotal();
        log.info("查询到的总条数：{}",total);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRows(list);

        return pageResult;
    }

    /**
     * @Author: 15041
     * @Date: 17:03 2021/1/8
     * @Parms: [setmeal, checkgroupIds]
     * @ReturnType: void
     *Description: 套餐-新增套餐
    */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("套餐-新增套餐service层传参,setmeal：{},checkgroupIds ：{}",setmeal,checkgroupIds);
        //将表单数据新增到setmeal表中
        setmealMapper.insertSelective(setmeal);
        //遍历数组，得到检查组id，将每个检查组id和套餐id新增到中间表中
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealCheckgroupMapper.insertSelective(setmealCheckgroup);
        }
    }

    /**
     * @Author: 15041
     * @Date: 20:41 2021/1/8
     * @Parms: [id]
     * @ReturnType: com.itheima.health.pojo.Setmeal
     *Description: 根据id查询套餐数据
    */
    @Override
    public Setmeal findById(Integer id) {
        log.info("套餐-根据id查询套餐数据service层传参：{}",id);
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(id);
        //设置img网址
        setmeal.setImg("http://qmi5v8lhm.hn-bkt.clouddn.com/"+setmeal.getImg());
        //用example 条件查询
        Example example = new Example(SetmealCheckgroup.class);
        Example.Criteria criteria = example.createCriteria();
        //创建中间表对象
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        //根据套餐id查询所有检查组id
        setmealCheckgroup.setSetmealId(setmeal.getId());
        criteria.andEqualTo(setmealCheckgroup);
        //返回中间表类型的包含所有检查组id的集合
        List<SetmealCheckgroup> list = setmealCheckgroupMapper.selectByExample(example);
        //创建检查组集合
        List<CheckGroup> group = new ArrayList<>();
        //遍历中间表集合，将得到的检查组id存入检查组集合中
        for (SetmealCheckgroup checkgroup : list) {
            CheckGroup cg = new CheckGroup();
            cg.setId(checkgroup.getCheckgroupId());
            group.add(cg);
        }
        //将检查组集合存入套餐实体类中
        setmeal.setCheckGroups(group);
        log.info("套餐-根据id查询套餐数据的结果：{}",setmeal);
        return setmeal;
    }

    /**
     * @Author: 15041
     * @Date: 10:29 2021/1/11
     * @Parms: [setmeal, checkgroupIds]
     * @ReturnType: void
     *Description: 根据id修改
    */
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("套餐-修改套餐数据传参service层传参 setmeal：{},checkgroupIds :{}",setmeal,checkgroupIds);
        //更新套餐数据
        setmealMapper.updateByPrimaryKeySelective(setmeal);
        //根据套餐id删除中间表中的套餐
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(setmeal.getId());
        int i = setmealCheckgroupMapper.selectCount(setmealCheckgroup);
        if (i>0){
            //删除中间表中的套餐
            setmealCheckgroupMapper.delete(setmealCheckgroup);
        }
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup checkgroup = new SetmealCheckgroup();
            checkgroup.setCheckgroupId(checkgroupId);
            checkgroup.setSetmealId(setmeal.getId());
            setmealCheckgroupMapper.insertSelective(checkgroup);
        }
    }
    /**
     * @Author: 15041
     * @Date: 10:28 2021/1/11
     * @Parms: [id]
     * @ReturnType: void
     *Description: 根据id删除
    */
    @Override
    public void delete(Integer id) {
        //根据id删除套餐
        setmealMapper.deleteByPrimaryKey(id);
        //删除套餐中的检查组
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        setmealCheckgroup.setSetmealId(id);
        //判断是否有检查组
        //获取套餐中所有的检查组
        int count = setmealCheckgroupMapper.selectCount(setmealCheckgroup);
        if (count>0){
            throw new BusinessException("该套餐还有检查组不可删除！！");
        }
        //反之删除中间表中的套餐
        setmealCheckgroupMapper.delete(setmealCheckgroup);
    }

    /**
     * @Author: 15041
     * @Date: 10:28 2021/1/11
     * @Parms: [fileName]
     * @ReturnType: int
     *Description: 根据文件名称统计数量
    */
    @Override
    public int count(String fileName) {
        Setmeal setmeal = new Setmeal();
        setmeal.setImg(fileName);
        return setmealMapper.selectCount(setmeal);
    }

    /**
     * @Author: 15041
     * @Date: 10:28 2021/1/11
     * @Parms: []
     * @ReturnType: java.util.List<com.itheima.health.pojo.Setmeal>
     *Description: 查询所有
    */
    @Override
    public List<Setmeal> findAll() {
        log.info("移动端-预约套餐查询所有");
        List<Setmeal> list = setmealMapper.selectAll();
        log.info("移动端-查询所有套餐数据：{}",list);
        return list;
    }
}
